#include "ShoppCart.h"
#include <cstring>
#include <iostream>

std::string GetTimeStr(time_t time)
{
    tm *m = localtime(&time);
    char s[1024] = {0};
    sprintf(s, "%d年%d月%d日 %d:%d:%d", m->tm_year + 1900, m->tm_mon + 1, m->tm_mday, m->tm_hour, m->tm_min, m->tm_sec);
    return std::string(s);
}

ShooppCart::~ShooppCart()
{
    while (!ShopCar.IsEmpty())
    {
        Goods *g = (Goods *)*ShopCar.GetFront();
        delete g;
        ShopCar.Pop();
    }
    while (!ReShopCar.IsEmpty())
    {
        Goods *g = (Goods *)*ReShopCar.GetTop();
        delete g;
        ReShopCar.Pop();
    }
}

void ShooppCart::AddGoods()
{
    Goods *g = new Goods("", 0);
    std::cout <<"请输入商品名称与价格:";
    std::cin >> g->Name >> g->price;
    g->buyTime = time(nullptr);
    ShopCar.Push(g);
    std::cout << "商品" << g->Name
              << "-----价格" << g->price
              << "-----" << GetTimeStr(g->buyTime)
              << " ——加入购物车" << std::endl;
    getchar();
    getchar();
}

void ShooppCart::DeleteGoods()
{
    std::cout << "请输入要删除的商品：";
    std::string DeleteGoods;
    std::cin >> DeleteGoods;
    LinkQueue tmpShopCar;
    while (!ShopCar.IsEmpty())
    {
        Goods *g = (Goods *)*ShopCar.GetFront();
        ShopCar.Pop();
        if (g->Name == DeleteGoods)
        {
            std::cout << "商品|" << g->Name << "删除成功" << std::endl;
            ReShopCar.Push(g);
        }
        else
        {
            tmpShopCar.Push(g);
        }
    }
    while (!tmpShopCar.IsEmpty())
    {
        ShopCar.Push(*tmpShopCar.GetFront());
        tmpShopCar.Pop();
    }
    getchar();
    getchar();
}

void ShooppCart::Revokedelet()
{
    // 把回收站的数据拿到购物车中
    std::cout << "请输入要恢复的商品" << std::endl;
    std::string RecoverName;
    std::cin >> RecoverName;
    LinkStack tmpReShopCar;
    while (!ReShopCar.IsEmpty())
    {
        Goods *g = (Goods *)*ReShopCar.GetTop();
        ReShopCar.Pop();
        g->buyTime = time(nullptr);
        if (g->Name == RecoverName)
        {
            std::cout << "商品|" << g->Name << "于" << GetTimeStr(g->buyTime) << "恢复成功" << std::endl;
            ShopCar.Push(g);
        }
        else
        {
            tmpReShopCar.Push(g);
        }
    }
    while (!tmpReShopCar.IsEmpty())
    {
        ReShopCar.Push(*tmpReShopCar.GetTop());
        tmpReShopCar.Pop();
    }
    getchar();
    getchar();
}

void ShooppCart::EmptyShopCar()
{
    while (!ShopCar.IsEmpty())
    {
        Goods *g = (Goods *)*ShopCar.GetFront();
        ReShopCar.Push(g);
        ShopCar.Pop();
    }
    std::cout << "购物车已清空" << std::endl;
    getchar();
    getchar();
}

void ShooppCart::EmptyReshopCar()
{
    while (!ReShopCar.IsEmpty())
    {
        Goods *g = (Goods *)*ReShopCar.GetTop();
        delete g;
        ReShopCar.Pop();
    }
    std::cout << "回收站已清空" << std::endl;
    getchar();
    getchar();
}

void ShooppCart::ShowShopCar()
{
    int count = 0;
    std::cout << "购物车商品列表" << std::endl;
    if (ShopCar.IsEmpty())
    {
        std::cout << "购物车内无商品" << std::endl;
    }
    LinkQueue tmpShopCar;
    while (ShopCar.IsEmpty() == false)
    {
        tmpShopCar.Push(*ShopCar.GetFront());
        ShopCar.Pop();
    }
    while (tmpShopCar.IsEmpty() == false)
    {
        Goods *g = (Goods *)*tmpShopCar.GetFront();
        count += g->price;
        std::cout << "购物车商品列表" << std::endl;
        std::cout << "商品名称" << g->Name << "-----商品价格" << g->price << "------商品加入时间" << GetTimeStr(g->buyTime) << std::endl;
        ShopCar.Push(g);
        tmpShopCar.Pop();
    }
    std::cout << "商品总价格----------" << count << std::endl;
    getchar();
    getchar();
}

void ShooppCart::ShowReShopCar()
{
    int count = 0;
    std::cout << "回收站商品列表" << std::endl;
    if (ReShopCar.IsEmpty())
    {
        std::cout << "回收站内无商品" << std::endl;
    }
    LinkStack tmpReShopCar;
    while (!ReShopCar.IsEmpty())
    {
        tmpReShopCar.Push(*ReShopCar.GetTop());
        ReShopCar.Pop();
    }
    while (!tmpReShopCar.IsEmpty())
    {
        Goods *g = (Goods *)*tmpReShopCar.GetTop();
        std::cout << "商品名称" << g->Name << "-----商品价格" << g->price << std::endl;
        ReShopCar.Push(g);
        tmpReShopCar.Pop();
    }
    getchar();
    getchar();
}
